/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpe.cin.dsoa.visualvm;

import com.sun.tools.visualvm.application.Application;
import com.sun.tools.visualvm.core.ui.DataSourceView;
import com.sun.tools.visualvm.core.ui.DataSourceViewProvider;
import com.sun.tools.visualvm.core.ui.DataSourceViewsManager;
import br.ufpe.cin.dsoa.visualvm.gui.PrincipalView;
import com.sun.tools.visualvm.tools.jmx.JmxModelFactory;
import java.io.IOException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

/**
 *
 * @author rafael.lima
 */
public class Facade extends DataSourceViewProvider<Application> {

    private static DataSourceViewProvider instance = new Facade();

    static public void initialize() {
        DataSourceViewsManager.sharedInstance().addViewProvider(instance, Application.class);
    }

    static public void unregister() {
        DataSourceViewsManager.sharedInstance().removeViewProvider(instance);
    }

    @Override
    public boolean supportsViewFor(Application application) {

        String mbeanName = "management:type=Agent";
        boolean ret = false;

        try {
            ret = JmxModelFactory.getJmxModelFor(application).getMBeanServerConnection().isRegistered(new ObjectName(mbeanName));
        } catch (MalformedObjectNameException ex) {
            //Exceptions.printStackTrace(ex);
        } catch (NullPointerException ex) {
            //Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            //Exceptions.printStackTrace(ex);
        }




        return ret;
    }

    @Override
    protected DataSourceView createView(Application application) {
        return new PrincipalView(application);
    }
}
